﻿using System;
using COM.DTO;
using NHibernate;
using COM.Logging;
using NHibernate.Criterion;
using System.Collections.Generic;

namespace DAL.DAO
{
    class DiscountDAO
    {
        public static void Add(Discount discount)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(discount);
                    transaction.Commit();
                }
            }
        }
        public static void Update(Discount discount)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Update(discount);
                    transaction.Commit();
                }
            }
        }

        public static void Remove(Discount discount)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(discount);
                    transaction.Commit();
                }
            }
        }

        public static Discount GetDiscountById(String discountId)
        {
            using (ISession session = NHibernateHelper.OpenSession())
                return session.Get<Discount>(discountId);
        }
        public static List<Discount> GetDiscountListByPriceId(string priceId)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                var discountList = session
                    .CreateCriteria(typeof(List<Discount>))
                    .Add(Restrictions.Eq("PriceId", priceId))
                    .UniqueResult<List<Discount>>();
                return discountList;
            }

        }
    }
}
